﻿//using System;
//using System.Text;
//using System.Collections.Generic;
//using System.Linq;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
//using Moq;
//using Microsoft.WindowsAzure.StorageClient;

//namespace AzureGames.Data.Implementation.Tests
//{
//    [TestClass]
//    public class BoardRepositoryTests
//    {
//        [TestMethod]
//        public void AddGameBoardRepository_ReturnsGuid()
//        {
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            //Act
//            var result = rep.AddGameBoard(new Model.Board { PlayerIDs = new List<string> { Guid.NewGuid(), Guid.NewGuid() } });
//            //Assert
//            Assert.IsNotNull(result);
//        }

//        [TestMethod]
//        public void AddGameBoardRepository_CallsTableRepAdd()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            //Act
//            var result = rep.AddGameBoard(new Model.Board());
//            //Assert
//            mockBoardTableRep.Verify(n => n.Add(It.IsAny<TableServiceEntity>()), Times.Once());
//        }

//        [TestMethod]
//        public void AddGameBoardRepository_CallsBoardPropRepOnceForEachProp()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Model.Board b = new Model.Board
//            {
//                Properties = new List<string> { "2", "545", "455" }
//            };
//            //Act
//            var result = rep.AddGameBoard(b);
//            //Assert
//            mockBoardPropTableRep.Verify(n => n.Add(It.IsAny<TableServiceEntity>()), Times.Exactly(3));
//        }

       

//        [TestMethod]
//        public void UpdateGameBoardRepository_CallsTableRepUpdate()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Model.Board board = new Model.Board { ID = Guid.NewGuid()} ;
//            mockBoardTableRep.Setup(n => n.Get("Board", board.ID.ToString())).
//                Returns(new BoardEntity());
                
//            //Act
//            var result = rep.UpdateGameBoard(board);
//            //Assert
//            mockBoardTableRep.Verify(n => n.Update(It.IsAny<TableServiceEntity>()), Times.Once());
//        }

//        [TestMethod]
//        public void UpdateGameBoardRepository_CallsBoardPropRepUpdateOnceForEachBoardProp()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Model.Board board = new Model.Board 
//            {
//                ID = Guid.NewGuid(),
//                Properties = new List<string> { "454", "54545", "5454", "3124124"}
//            };
//            mockBoardTableRep.Setup(n => n.Get("Board", board.ID.ToString())).
//                Returns(new BoardEntity());

//            List<TableServiceEntity> list = new List<TableServiceEntity> 
//            {
//                 new BoardPropertyEntity { BoardID = board.ID },
//                 new BoardPropertyEntity { BoardID = board.ID },
//                 new BoardPropertyEntity { BoardID = board.ID },
//                 new BoardPropertyEntity { BoardID = board.ID }
//            };
//            mockBoardPropTableRep.Setup(n => n.GetListByNamedProperty("BoardProperty", "BoardID", board.ID))
//                .Returns(list);


//            //Act
//            var result = rep.UpdateGameBoard(board);
//            //Assert
//            mockBoardPropTableRep.Verify(n => n.Update(It.IsAny<TableServiceEntity>()), Times.Exactly(list.Count));
//        }

//        [TestMethod]
//        public void GetGameBoard_GetsBoardWhenInAzureRep()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Guid BoardID = Guid.NewGuid();
//            BoardEntity be = new BoardEntity { RowKey = BoardID.ToString(), PartitionKey = "Board", GameDefinitionID = 1, ID = BoardID};
//            mockBoardTableRep.Setup(n => n.Get("Board", BoardID.ToString())).
//                Returns(be);

//            //Act
//            var result = rep.GetGameBoard(BoardID);
//            //Assert

//            Assert.IsNotNull(result);
//            Assert.IsTrue(result.ID == BoardID);

//        }

//        [TestMethod]
//        public void GetGameBoard_GetsBoardWhenInAzureRepAndPopulatesUserCorrectly()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Guid BoardID = Guid.NewGuid();
//            Guid player1 = Guid.NewGuid();
//            Guid player2 = Guid.NewGuid();
//            Guid player3 = Guid.NewGuid();

//            BoardEntity be = new BoardEntity { RowKey = BoardID.ToString(), PartitionKey = "Board", GameDefinitionID = 1, ID = BoardID,
//             PlayerIDs = player1.ToString() + "|" + player2.ToString() + "|" + player3.ToString()};

//            mockBoardTableRep.Setup(n => n.Get("Board", BoardID.ToString())).
//                Returns(be);

//            //Act
//            var result = rep.GetGameBoard(BoardID);
//            //Assert

//            Assert.IsNotNull(result.PlayerIDs);
//            Assert.IsTrue(result.PlayerIDs.Contains(player1));
//            Assert.IsTrue(result.PlayerIDs.Contains(player2));
//            Assert.IsTrue(result.PlayerIDs.Contains(player3));
//        }

//        [TestMethod]
//        public void GetGameBoard_GetsBoardAndPropsWhenInAzureRep()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Guid BoardID = Guid.NewGuid();
//            BoardEntity be = new BoardEntity { RowKey = BoardID.ToString(), PartitionKey = "Board", GameDefinitionID = 1, ID = BoardID };
//            mockBoardTableRep.Setup(n => n.Get("Board", BoardID.ToString())).
//                Returns(be);
//            List<TableServiceEntity> list = new List<TableServiceEntity> 
//            {
//                 new BoardPropertyEntity { BoardID = BoardID, PropertyValue = "10" },
//                 new BoardPropertyEntity { BoardID = BoardID , PropertyValue = "testValue" },
//                 new BoardPropertyEntity { BoardID = BoardID, PropertyValue = "545" },
//                 new BoardPropertyEntity { BoardID = BoardID, PropertyValue = "erer"}
//            };
//            mockBoardPropTableRep.Setup(n => n.GetListByNamedProperty("BoardProperty", "BoardID", BoardID))
//                .Returns(list);

//            //Act
//            var result = rep.GetGameBoard(BoardID);
//            //Assert

//            Assert.IsNotNull(result);
//            Assert.IsTrue(result.Properties.Count == list.Count);
//            Assert.IsTrue(result.Properties[1] == "testValue");
//        }

//        [TestMethod]
//        public void GetActiveGames_CallsGetListByNamedProperty()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Guid BoardID = Guid.NewGuid();
//            Guid player1 = Guid.NewGuid();
//            Guid player2 = Guid.NewGuid();
//            Guid player3 = Guid.NewGuid();
//            List<TableServiceEntity> list = new List<TableServiceEntity>();
//            BoardEntity be = new BoardEntity { 
//                RowKey = BoardID.ToString(),
//                PartitionKey = "Board",
//                GameDefinitionID = 1,
//                ID = BoardID,
//                PlayerIDs = player1.ToString().ToUpper() + "|" + player2.ToString() + "|" + player3.ToString()
//            };
//            list.Add(be);


//            int gameDefinitionID = 2;
//            mockBoardTableRep.Setup(n => n.GetListByNamedProperty("Board", "GameDefinitionID", gameDefinitionID))
//                .Returns(list);

//            //Act
//            var result = rep.GetActiveGames(gameDefinitionID, player3);
//            //Assert

//            mockBoardTableRep.Verify(n => n.GetListByNamedProperty("Board", "GameDefinitionID", gameDefinitionID), Times.Exactly(1));
            
//        }

//        [TestMethod]
//        public void GetActiveGames_ReturnsOnlyActiveAndCreatedBoards()
//        {
//            //Arrange
//            var mockBoardTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            var mockBoardPropTableRep = new Mock<IAzureTableRepository<TableServiceEntity>>();
//            IBoardRepository rep = new BoardRepository(mockBoardTableRep.Object, mockBoardPropTableRep.Object);
//            Guid BoardID = Guid.NewGuid();
//            Guid BoardID2 = Guid.NewGuid();

//            Guid player1 = Guid.NewGuid();
//            Guid player2 = Guid.NewGuid();
//            Guid player3 = Guid.NewGuid();
//            List<TableServiceEntity> list = new List<TableServiceEntity>();

//            BoardEntity inactivebe = new BoardEntity
//            {
//                RowKey = BoardID.ToString(),
//                PartitionKey = "Board",
//                GameDefinitionID = 1,
//                ID = BoardID,
//                PlayerIDs = player1.ToString().ToUpper() + "|" + player2.ToString() + "|" + player3.ToString(),
//                State = 2
//            };

//            BoardEntity activebe = new BoardEntity
//            {
//                RowKey = BoardID2.ToString(),
//                PartitionKey = "Board",
//                GameDefinitionID = 2,
//                ID = BoardID2,
//                PlayerIDs = player1.ToString().ToUpper() + "|" + player2.ToString() + "|" + player3.ToString(),
//                State = 0
//            };
//            list.Add(inactivebe);
//            list.Add(activebe);

//            int gameDefinitionID = 2;
//            mockBoardTableRep.Setup(n => n.GetListByNamedProperty("Board", "GameDefinitionID", gameDefinitionID))
//                .Returns(list);

//            //Act
//            var result = rep.GetActiveGames(gameDefinitionID, player3);
//            //Assert

//            Assert.IsTrue(result.Count() == 1);
//            Assert.IsTrue(result.First().ID  == BoardID2);
            
//        }
//    }
//}
